package org.hit.burkun.model;

import java.util.Collection;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class Content {
	private final String mid;
	private final String orginalStr;
	private Collection<String> urls;
	private Collection<String> atSomeOne;
	private Collection<String> topics;
	private Collection<String> otherTopic;
	// private int numberOfGanTan; //感叹号
	// private int numberOfWeihao; //问号
	// private int numberOfContinueDot; //连续的点号
	// private Collection<String> enmojjs; //emojj符号
	private String cleanStr;

	private static Pattern atNamePattern = Pattern.compile("@([\\u4e00-\\u9fa5a-zA-Z\\d_-]{1,30})");
	private static Pattern urlPattern = Pattern.compile("(http:[A-Za-z0-9/.\\?&%]+)");
	// private static Pattern emojiPattern; //没做。。。。
	private static Pattern topicPattern = Pattern.compile("#(.*?)#");
	private static Pattern otherTopicPattern = Pattern
			.compile("【(.*)】|《(.*)》|『(.*)』");
	public Content(String mid, String orginalStr){
		this.mid = mid;
		this.orginalStr = orginalStr;
		init();
	}


	private void init(){
		// 提取@后面的人物名字
		urls = getResultByPattern(orginalStr, urlPattern);
		atSomeOne = getResultByPattern(orginalStr, atNamePattern);
		topics = getResultByPattern(orginalStr, topicPattern);
		//emojj...
		otherTopic = getResultByPattern(orginalStr, otherTopicPattern);
		cleanStr = cleanStr(orginalStr);
	}

	public String getOrginalStr() {
		return orginalStr;
	}


	public Collection<String> getUrls() {
		return urls;
	}


	public Collection<String> getTopics() {
		return topics;
	}


	public Collection<String> getOtherTopic() {
		return otherTopic;
	}

	public Collection<String> getAtSomeOne(){
		return atSomeOne;
	}

	public String getMid(){
		return mid;
	}

	private static String cleanStr(String line){
		String newLine = line.replaceAll("http:[A-Za-z0-9/.\\?&%]+", "")
				.replaceAll("@[\\u4e00-\\u9fa5a-zA-Z\\d_-]{1,30}", "")
				.replaceAll("【|#|】|\\[|\\]|《|》|『|』|!|！|？|\\?", "")
				.replaceAll("[\\pP‘’“”]", " ").trim();// 去掉url
		return newLine;
	}

	private static Collection<String> getResultByPattern(String line, Pattern p){
		Matcher m = p.matcher(line);
		Collection<String> res = new LinkedList<String>();
		while(m.find()){
			int count = m.groupCount();
			for(int i=1; i<=count; i++){
				if(m.group(i) != null){
					res.add(m.group(i));
				}

			}
		}
		return res;
	}

	public static double getEditSim(Content c1, Content c2){
		return editDistance(c1.getCleanStr(), c2.getCleanStr());
	}

	public String getCleanStr(){
		return cleanStr;
	}


	private static double editDistance(String arg1, String arg2){
		// 编辑距离
		//dp = min(dp[i][j-1] + 1, dp[i-1][j] + 1, dp[i-1][j-1] + 0/1)
		arg1 = arg1.trim();
		arg2 = arg2.trim();
		if(arg1.length() == 0 && arg2.length() == 0){
			return 0;
		}
		int [][] dp = new int[arg1.length() + 1][arg2.length() + 1];
		for(int i=0; i<=arg1.length(); i++){
			dp[i][0] = i;
		}
		for(int j=0; j<=arg2.length(); j++){
			dp[0][j] = j;
		}
		for(int i=1; i<=arg1.length(); i++){
			for(int j=1; j<=arg2.length(); j++){
				int minn = Math.min(dp[i-1][j], dp[i][j-1]) + 1;
				if(arg1.charAt(i-1) == arg2.charAt(j-1)){
					dp[i][j] = Math.min(dp[i-1][j-1], minn);
				}else{
					dp[i][j] = Math.min(dp[i-1][j-1] + 1, minn);
				}
			}
		}
		return 1- (dp[arg1.length()][arg2.length()] + 0.0)/Math.max(arg1.length(), arg2.length());
	}


	public static void main(String[] args){
		String res = "@@Hello酷狗） @搜狗) @QQ拼音, 百度知道 @@1233？@abcv[]@1231#@adads:@eee-$";
		System.out.println(cleanStr(res));
		getResultByPattern(res, atNamePattern);
	}

}
